#version 400
in vec3 Position;

uniform sampler2D backgroundTex;

layout (location = 0) out vec4 FragColor;
in vec2 tcoord;

uniform int sourcex;
uniform int sourcey;
uniform int destinationx;
uniform int destinationy;

uniform int windowWidth;
uniform int windowHeight;

void main()
{	

	int xDistance = abs(sourcex - destinationx);
	int yDistance = abs(sourcey - destinationy);

	float change_in_y_radius = float(yDistance);
	float change_in_x_radius = float(xDistance);

	//Radius between destination and source
	float clickRadius =  float(sqrt(float(destinationx - sourcex)*float(destinationx - sourcex) + 
							float(destinationy - sourcey)*float(destinationy - sourcey)));

	//Radius between frag and source
	float fragRadius = float(sqrt(float(gl_FragCoord.x - sourcex)*float(gl_FragCoord.x - sourcex) + 
						   float(gl_FragCoord.y - sourcey)*float(gl_FragCoord.y - sourcey)));

	float xDisplacment = (change_in_x_radius - fragRadius) * ((clickRadius - fragRadius) / clickRadius);
	float xRatio = xDisplacment / float(windowWidth);

	float yDisplacment = (change_in_y_radius - fragRadius) * ((clickRadius - fragRadius) / clickRadius);
	float yRatio = yDisplacment / float(windowHeight);

	float destinationRadius = clickRadius;
	float destinationAngle = atan(float(destinationy - sourcey),float(destinationx - sourcex));

	float fragmentRadius = fragRadius;
	float fragmentAngle = atan(float(gl_FragCoord.y - sourcey),float(gl_FragCoord.x - sourcex));

	//if (destinationAngle < 0.0){FragColor.r = 1.0;}
	//if (destinationAngle < 0.0){FragColor.g = -1 * destinationAngle;}
	//FragColor.r = destinationAngle;

	bool inTriangle = false;

	if (fragmentAngle >= (destinationAngle + float(3.9269)) && fragmentAngle <= (destinationAngle + float(2.35619)))
		inTriangle = true;

	if ((fragRadius <= change_in_x_radius) )//&& inTriangle)
	{
		FragColor.r = xRatio;

		// add negative value
		if (destinationx - sourcex >= 0)
		{
			if (destinationy - sourcey >= 0)
				FragColor.b = 0.0;
			else
				FragColor.b = 0.75;	
		}
		else
		{
			if (destinationy - sourcey >= 0)
				FragColor.b = 0.25;
			else
				FragColor.b = 0.5;	
		}
		
	}

	if ((fragRadius <= change_in_y_radius)) //&& inTriangle)
	{
		FragColor.g = yRatio;

		// add negative value
		if (destinationx - sourcex >= 0)
		{
			if (destinationy - sourcey >= 0)
				FragColor.b = 0.0;
			else
				FragColor.b = 0.75;	
		}
		else
		{
			if (destinationy - sourcey >= 0)
				FragColor.b = 0.25;
			else
				FragColor.b = 0.5;	
		}

	}

}
